package com.ontheroad.controller;

import cn.modoumama.page.GridDataModel;
import com.ontheroad.activity.model.ActivityTypeModel;
import com.ontheroad.activity.service.ActivityTypeService;
import com.ontheroad.banner.model.BannerModel;
import com.ontheroad.core.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/27.
 */
@Controller
    @RequestMapping("activity/type")
public class ActivityTypeController extends BaseController {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private ActivityTypeService activityTypeService;

    @RequestMapping("init")
    public String init() {
        return "activity/type/init";
    }

    @RequestMapping("getData")
    @ResponseBody
    public GridDataModel getData(HttpServletRequest request) {
        return getGridData(request, activityTypeService);
    }


    @RequestMapping("toAdd")
    public String toAdd(Map map) {
        return "activity/type/add";
    }

    @RequestMapping("save")
    public String save(ActivityTypeModel typeModel, Map map) {
        try {
            typeModel.setCreateTime(new Date());
            activityTypeService.insert(typeModel);
        } catch (Exception e) {
            log.error("保存活动类型出错：", e);
            map.put("optionResult", 2);
            map.put("faildMessage", "保存活动类型出错");
        }
        return SUCCESS;
    }

    @RequestMapping("edit")
    public String edit(Integer id, Map map) {
        ActivityTypeModel typeModel = activityTypeService.findById(id);
        if (typeModel == null) {
            return FAILD;
        }
        map.put("model", typeModel);
        return "activity/type/edit";
    }

    @RequestMapping("update")
    public String update(ActivityTypeModel typeModel, Map map) {
        try {
            activityTypeService.updateById(typeModel);
        } catch (Exception e) {
            log.error("更新活动类型出错：", e);
            map.put("optionResult", 2);
            map.put("faildMessage", "更新活动类型出错");
        }
        return SUCCESS;
    }

    @RequestMapping(value = "delete")
    public void delete(HttpServletRequest request, PrintWriter out) {
        log.debug("method: delete() ");
        String msg = "操作成功";
        boolean result = true;
        try {
            String ids = request.getParameter("ids");
            String[] aDeleteId = ids.split(",");
            for (String id : aDeleteId) {
                activityTypeService.removeById(Integer.valueOf(id));
            }
        } catch (Exception e) {
            msg = "系统发生异常！";
            result = false;
        }
        ajaxJsonResponse(out, result, msg);
    }
}
